package com.jyw.common.config;

import com.plumelog.core.LogMessageThreadLocal;
import com.plumelog.core.TraceId;
import com.plumelog.core.TraceMessage;
import com.plumelog.trace.aspect.AbstractAspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * @author 沈峻
 * @ClassName 链路追踪切面全局打点
 * @Description TODO
 * @Date 2023/3/27 15:30
 **/

@Aspect
@Slf4j
@Component
public class AspectConfig extends AbstractAspect {

    @Autowired
    private Tracer tracer;


    @Around("within(com.jyw..*.controller..*))")//这边写自己的包的路径
    public Object around(JoinPoint joinPoint) throws Throwable {
        if(ObjectUtils.isEmpty(tracer.currentSpan())){
            return aroundExecute(joinPoint);
        }
        String traceId = tracer.currentSpan().context().traceId();
        TraceId.logTraceID.set(traceId);
        TraceMessage traceMessage = LogMessageThreadLocal.logMessageThreadLocal.get();
        if(!ObjectUtils.isEmpty(traceMessage)){
            traceMessage.setTraceId(traceId);
        }
        return aroundExecute(joinPoint);
    }
}
